package object.obj2D.bullet;

import javax.microedition.khronos.opengles.GL10;

import object.obj2D.graphics.AnimSystem;
import object.obj2D.graphics.Square;
import system.Graphics;


public class FGunBullet extends IBullet {
	private GL10 gl;
	
	private float m_width = 0.4f;
	private float m_height = 0.4f;
	private AnimSystem m_bullet = new AnimSystem("/res/raw/explode.bin", 0, -1);
	private Square m_bulletSquare = new Square(m_width, m_height);
	private int m_iBulletFrame = 4;
	
	private static final float TRANS_X = 0.45f;
	private static final float TRANS_Y = -0.5f;
	private static final float TRANS_Z = -2;
	
	private float m_transX = TRANS_X;
	private float m_transY = TRANS_Y;
	private float m_transZ = TRANS_Z;
	
	private float m_dTransX = TRANS_X/10;
	private float m_dTransY = TRANS_Y/10;
	private float m_dTransZ = 1;
	
	private boolean isDisplay = false;


	public FGunBullet()
	{
		gl = Graphics.GetGraphics();
	}
	
	public void SetAlpha(float alpha)
	{
		m_alpha = alpha;
	}
	
	public void SetBeta(float beta)
	{
		m_beta = beta;
	}
	
	public void SetDistance(float distance)
	{
		m_distance = distance;
		m_dTransX = TRANS_X / (distance + TRANS_Z);
		m_dTransY = TRANS_Y / (distance + TRANS_Z);
		m_dTransZ = 1;
	}
	
	public float GetAlpha()
	{
		return m_alpha;
	}
	
	public float GetBeta()
	{
		return m_beta;
	}
	
	public float GetDistance()
	{
		return m_distance;
	}
	
	public void Update()
	{
		if(isDisplay)
		{
			m_transX -= m_dTransX;
			m_transY -= m_dTransY;
			m_transZ -= m_dTransZ;

			if(m_transZ <= -m_distance)
			{
				m_transX = TRANS_X;
				m_transY = TRANS_Y;
				m_transZ = TRANS_Z;
				
				m_bullet.Stop();
				isDisplay = false;
			}
		}
	}
	
	public void Start()
	{
		isDisplay = true;
		m_bullet.Play();
	}
	
	public void Stop()
	{
		m_transX = TRANS_X;
		m_transY = TRANS_Y;
		m_transZ = TRANS_Z;
		
		m_bullet.Stop();
		isDisplay = false;
	}
	
	public boolean isPlaying()
	{
		return isDisplay;
	}
	
	public void Render()
	{
		if(isDisplay)
		{
			gl.glMatrixMode(GL10.GL_MODELVIEW);
			gl.glPushMatrix();
			gl.glRotatef(180 + m_beta, 0, 1.0f, 0);
			gl.glRotatef(90 - m_alpha, 1.0f, 0, 0);
			gl.glTranslatef(0, 0, m_transZ);
			gl.glTranslatef(m_transX, 0, 0);
			gl.glTranslatef(0, m_transY, 0);
			m_bullet.setPlayFrame(m_iBulletFrame);
			m_bullet.Render();
			
			m_bulletSquare.Render();
			gl.glRotatef(-90, 0, 1.0f, 0);
			m_bulletSquare.Render();
			
			gl.glPopMatrix();
		}
	}
}
